package com.example.main;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeoutException;

public class RoutingProducer {
    /**
     * 生产者 → 消息队列
     * 创建连接工厂,并设置参数
     * 创建连接 Connection
     * 创建通道 Channel
     * ----------------
     * 创建交换机
     * 创建队列
     * 交换机绑定到队列
     * <p>
     * 发送消息
     */
    //定义交换机名称
    private static final String ROUTING_EXCHANGE_NAME = "my_routing_exchange";
    //定义一个 error 队列,仅有 error 的日志到这个队列
    private static final String ERROR_QUEUE_NAME = "my_error_queue";
    //定义一个 all 队列, error info warning 级别的日志都到这个队列
    private static final String ALL_QUEUE_NAME = "my_all_queue";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        if (true) {
            factory.setHost("127.0.0.1");
            factory.setPort(5672);
            factory.setUsername("guest");
            factory.setPassword("guest");
            factory.setVirtualHost("/");
        }
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //创建交换机,使用路由模式的交换机
        channel.exchangeDeclare(ROUTING_EXCHANGE_NAME, BuiltinExchangeType.DIRECT, true, false, false, null);
        //创建队列
        channel.queueDeclare(ERROR_QUEUE_NAME, true, false, false, null);
        channel.queueDeclare(ALL_QUEUE_NAME, true, false, false, null);

        //绑定交换机
        /**
         * String queue                 :队列名称
         * String exchange              :交换机名称
         * String routingKey            :路由键,fanout 广播模式不需要路由键
         * Map<String, Object> arguments:参数
         */
        channel.queueBind(ERROR_QUEUE_NAME, ROUTING_EXCHANGE_NAME, "error");

        channel.queueBind(ALL_QUEUE_NAME, ROUTING_EXCHANGE_NAME, "error");
        channel.queueBind(ALL_QUEUE_NAME, ROUTING_EXCHANGE_NAME, "info");
        channel.queueBind(ALL_QUEUE_NAME, ROUTING_EXCHANGE_NAME, "warning");

        //发送短信
        String[] keys = {"error", "info", "warning"};
        int errorCount = 0;
        int infoCount = 0;
        int warningCount = 0;
        for (int i = 0; i < 30; i++) {
            int random = (int) (Math.random() * (3 - 1 + 1)) + 0;   //生成0,1,2随机数
            String logLevel = keys[random];

            String str = "我是 " + logLevel + "\t消息\t" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss_SSS").format(new Date());
            System.out.println("发送消息:\t" + str);
            channel.basicPublish(ROUTING_EXCHANGE_NAME, logLevel, null, str.getBytes());

            if (random == 0) {
                errorCount++;
            } else if (random == 1) {
                infoCount++;
            } else if (random == 2) {
                warningCount++;
            }
        }

        System.out.println("error\t共计: " + errorCount + "条");
        System.out.println("info\t共计: " + infoCount + "条");
        System.out.println("warning\t共计: " + warningCount + "条");

        // 关闭资源
        channel.close();
        connection.close();
    }
}
